
// uuidPlugin.ts
import type { App } from 'vue';
// 首先，保持原始UUID类的实现不变
class UUID {
  id: string;
  constructor() {
      this.id = this.createUUID();
    }
  userDefinedUUID(value: string): string {
        return value + '_' + this.id;
  }
  valueOf(): string {
        return this.id;
    }
   
  toString(): string {
        return this.id;
    }
  createUUID(): string {
        const dg = new Date(1582, 10, 15, 0, 0, 0, 0);
        const dc = new Date();
        const t = dc.getTime() - dg.getTime();
        var h = '';
        var tl = getIntegerBits(t, 0, 31);
        var tm = getIntegerBits(t, 32, 47);
        var thv = getIntegerBits(t, 48, 59) + '1';
        var csar = getIntegerBits(rand(4095), 0, 7);
        var csl = getIntegerBits(rand(4095), 0, 7);
       
        var n = getIntegerBits(rand(8191), 0, 7) +  
                getIntegerBits(rand(8191), 8, 15) +  
                getIntegerBits(rand(8191), 0, 7) +  
                getIntegerBits(rand(8191), 8, 15) +  
                getIntegerBits(rand(8191), 0, 15);  
        return tl + h + tm + h + thv + h + csar + csl + h + n;
    }
}

// 将静态方法移到类外面，以便在插件安装时直接使用
function getIntegerBits(val: any, start: number, end: number): string {
  // ...（保留getIntegerBits方法逻辑）
  var base16 = returnBase(val,16); 
    var quadArray = new Array(); 
    var quadString = ''; 
    var i = 0; 
    for(i=0;i<base16.length;i++){ 
        quadArray.push(base16.substring(i,i+1));     
    } 
    for(i=Math.floor(start/4);i<=Math.floor(end/4);i++){ 
        if(!quadArray[i] || quadArray[i] == '') quadString += '0'; 
        else quadString += quadArray[i]; 
    } 
    return quadString; 
}

function returnBase(number: any, base: any): string {
  // ...（保留returnBase方法逻辑）
  var convert = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; 
  if (number < base) var output = convert[number]; 
  else { 
      var MSD:any = '' + Math.floor(number / base); 
      var LSD = number - MSD*base; 
      if (MSD >= base) var output = returnBase(MSD,base) + convert[LSD]; 
      else var output = convert[MSD] + convert[LSD]; 
  } 
  return output; 
}

function rand(max: number): number {
  // ...（保留rand方法逻辑）
  return Math.floor(Math.random() * max); 
}

// 定义Vue 3插件
const UUIDPlugin = {
  install(app: App) {
    // 在安装插件时，创建一个UUID实例并将其挂在到全局原型上
    const uuidInstance = new UUID();
    app.config.globalProperties.$uuid = uuidInstance;
    // 或者，如果希望直接挂载方法，可以这样操作：
    // app.config.globalProperties.userDefinedUUID = (value: string) => uuidInstance.userDefinedUUID(value);
    // app.config.globalProperties.valueOf = () => uuidInstance.valueOf();
    // app.config.globalProperties.toString = () => uuidInstance.toString();
  }
};

export default UUIDPlugin;
